import re
import subprocess
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import plotly.graph_objs as go
import gpxpy
import random
from dateutil.parser import parse
from copy import deepcopy
from imageio import imread
from matplotlib.pyplot import imshow
from plotly.offline import init_notebook_mode, iplot
init_notebook_mode(connected=True)
%matplotlib inline
img = imread('Downloads/basemap.png') / 255
plt.figure(figsize = (10,10))
imshow(img)
print(f"Size: {img.shape[0]}x{img.shape[1]}x{img.shape[2]} {img.dtype}")
!pip install gpxpy
def show_colors(colors):
# colors_matrix = np.reshape(colors, [4, n_colors // 4, 3])
imshow(np.reshape(colors, (1, -1, 3)), aspect='auto')
plt.xticks([])
plt.yticks([])
plt.gcf().set_size_inches(10, 1)
show_colors(colors)
# Find and show unique colors
img_array = img[:, :, :3].reshape((img.shape[0] * img.shape[1], 3))
colors = np.unique(img_array, axis=0)
n_colors = colors.shape[0]
def show_colors(colors):
# colors_matrix = np.reshape(colors, [4, n_colors // 4, 3])
imshow(np.reshape(colors, (1, -1, 3)), aspect='auto')
plt.xticks([])
plt.yticks([])
plt.gcf().set_size_inches(10, 1)
show_colors(colors)
# Create a custom colormap
color_to_value = {tuple(color[:3]): i / (n_colors - 1) for i, color in enumerate(colors)}
my_cmap_ply = [(value, 'rgb({}, {}, {})'.format(*color)) for color, value in color_to_value.items()]
# Map pixels to values
fun_find_value = lambda x: color_to_value[tuple(x[:3])]
values = np.apply_along_axis(fun_find_value, 2, np.flipud(img))
# Display terrain
yy = np.linspace(0, 1, img.shape[0])
xx = np.linspace(0, 1, img.shape[1])
zz = np.zeros(img.shape[:2])
surf = go.Surface(
x=xx, y=yy, z=zz,
colorscale=my_cmap_ply,
surfacecolor=values,
showscale=False
)
fig = go.Figure(data=[surf], layout=go.Layout())
iplot(fig, filename='terrain.html')